﻿//Copyright 2010 http://tv4home.codeplex.com
//This file is part of TV4Home.WebInterface.UI.
//TV4Home.WebInterface.UI is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
//TV4Home.WebInterface.UI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//You should have received a copy of the GNU General Public License along with TV4Home.WebInterface.UI If not, see http://www.gnu.org/licenses/
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using TV4Home.Server.TVEInteractionLibrary.Interfaces;

namespace TV4Home.WebInterface.UI.Code
{
    /// <summary>
    /// Contains settings for the website. The settings are stored in the TVE database.
    /// </summary>
    public static class WebsiteSettings
    {
        #region Fields
        // database setting key names
        private const string SETTING_SHOWCHANNELNAMEIFCHANNELPICTUREISPRESENT = "TV4Home_WebInterface_ShowChannelNameIfChannelPictureIsPresent";
        private const string SETTING_TVGUIDEHOURSTOSHOW = "TV4Home_WebInterface_TVGuideHoursToShow";
        private const string SETTING_WEBSERVICEACCESSCODE = "TV4Home_WebInterface_WebServiceAccessCode";
        #endregion

        #region Properties
        public static bool ShowChannelNameIfChannelPictureIsPresent
        {
            get
            {
                // set default value
                if (String.IsNullOrEmpty(WebService.TV4HomeWebService.ServiceInstance.ReadSettingFromDatabase(SETTING_SHOWCHANNELNAMEIFCHANNELPICTUREISPRESENT)))
                    ShowChannelNameIfChannelPictureIsPresent = false;

                return Convert.ToBoolean(WebService.TV4HomeWebService.ServiceInstance.ReadSettingFromDatabase(SETTING_SHOWCHANNELNAMEIFCHANNELPICTUREISPRESENT));
            }
            set
            {
                WebService.TV4HomeWebService.ServiceInstance.WriteSettingToDatabase(SETTING_SHOWCHANNELNAMEIFCHANNELPICTUREISPRESENT, Convert.ToString(value));
            }
        }

        public static int TVGuideHoursToShow
        {
            get
            {
                // set default value
                if (String.IsNullOrEmpty(WebService.TV4HomeWebService.ServiceInstance.ReadSettingFromDatabase(SETTING_TVGUIDEHOURSTOSHOW)))
                    TVGuideHoursToShow = 3;

                return Convert.ToInt32(WebService.TV4HomeWebService.ServiceInstance.ReadSettingFromDatabase(SETTING_TVGUIDEHOURSTOSHOW));
            }
            set
            {
                WebService.TV4HomeWebService.ServiceInstance.WriteSettingToDatabase(SETTING_TVGUIDEHOURSTOSHOW, Convert.ToString(value));
            }
        }

        public static string WebServiceAccessCode
        {
            get
            {
                // set default value
                if (String.IsNullOrEmpty(WebService.TV4HomeWebService.ServiceInstance.ReadSettingFromDatabase(SETTING_WEBSERVICEACCESSCODE)))
                    WebServiceAccessCode = CreateRandomAccessCode(15);

                return WebService.TV4HomeWebService.ServiceInstance.ReadSettingFromDatabase(SETTING_WEBSERVICEACCESSCODE);
            }
            set
            {
                WebService.TV4HomeWebService.ServiceInstance.WriteSettingToDatabase(SETTING_WEBSERVICEACCESSCODE, value);
            }
        }
        #endregion

        #region Private Methods
        private static string CreateRandomAccessCode(int length)
        {
            Random rnd = new Random();

            const string numbers = "0123456789";
            const string lowercase = "abcdefghijklmnopqrstuvwxyz";
            const string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            const string special = "$?&=!%#";
            const string combinedChars = numbers + lowercase + special + uppercase;

            string code = "";

            for (int i = 0; i < length; i++)
            {
                code += combinedChars[rnd.Next(combinedChars.Length - 1)];
            }

            return code;
        }
        #endregion
    }
}
